﻿using System.Drawing;
using System.Windows.Forms;

namespace KPP
{
    public class DefaultTextTextBox : TextBox
    {
        public string DefaultText { get; set; }

        private static readonly Font Origin = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 204);

        private static readonly Font DefaultTextFont = new Font(Origin, FontStyle.Italic);
        private static readonly Font NormalTextFont = new Font(Origin, FontStyle.Regular);

        private static readonly Color DefaultColor = SystemColors.WindowFrame;
        private static readonly Color NormalColor = Color.Black;

        public bool HasText
        {
            get { return Font.Equals(NormalTextFont); }
        }

        public void SetDefaults()
        {
            Font = DefaultTextFont;
            ForeColor = DefaultColor;
        }

        protected override void OnTextChanged(System.EventArgs e)
        {
            base.OnTextChanged(e);
            Font = NormalTextFont;
            ForeColor = NormalColor;
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (Font.Equals(DefaultTextFont))
            {
                Text = "";
            }
        }

        protected override void OnLeave(System.EventArgs e)
        {
            base.OnLeave(e);
            if (Text == "")
            {
                Text = DefaultText;
                Font = DefaultTextFont;
                ForeColor = DefaultColor;
            }
        }
    }
}
